package com.leiou.stu_reg;


import com.google.gson.JsonObject;
import com.leiou.pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/selectinfo")
public class selectStuInfoHandler extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        用什么方式显示汉字
        resp.setContentType("text/html;charset=UTF-8");

        PrintWriter out = resp.getWriter();

        String username = req.getParameter("username");
        if (username == null || username.isEmpty()) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "缺少用户名参数");
            return;
        }
        try {
            stuRegDao stuRegDao=new stuRegDaoImpl();
            Student student = stuRegDao.selectStu(username);
            if (student != null) {
                // 构造JSON响应
                JsonObject json = new JsonObject();
                json.addProperty("studentId", student.getStudentId());
                json.addProperty("name", student.getName());
                json.addProperty("gender", student.getGender() != null ? student.getGender() : "");
                if (student.getBirthDate() != null) {
                    json.addProperty("birthDate", student.getBirthDate().toString());
                } else {
                    json.addProperty("birthDate", "");
                }
                String idCard = student.getIdCard();
                if (idCard != null && idCard.length() >= 7) {
                    json.addProperty("idCard", idCard.substring(0, 3) + "***********" + idCard.substring(idCard.length()-4));
                } else {
                    json.addProperty("idCard", "");
                }
                json.addProperty("college", student.getCollege() != null ? student.getCollege() : "未分配");
                json.addProperty("major", student.getMajor() != null ? student.getMajor() : "未分配");
                json.addProperty("className", student.getClassName() != null ? student.getClassName() : "未分配");
                String statusText;
                switch (student.getStatus()) {
                    case 0:
                        statusText = "在读";
                        break;
                    case 1:
                        statusText = "休学";
                        break;
                    case 2:
                        statusText = "毕业";
                        break;
                    case 3:
                        statusText = "退学";
                        break;
                    default:
                        statusText = "未知状态";
                }
                json.addProperty("status", statusText);
                json.addProperty("dormId", student.getDormId() != null ? "#" + student.getDormId() : "未分配");
                json.addProperty("bedNumber", student.getBedNumber() != null ? student.getBedNumber() : "");
                if (student.getPhone() != null && student.getPhone().length() >= 7) {
                    String phone = student.getPhone();
                    json.addProperty("phone", phone.substring(0, 3) + "****" + phone.substring(7));
                } else {
                    json.addProperty("phone", "");
                }
                if (student.getEmail() != null && student.getEmail().contains("@")) {
                    String[] emailParts = student.getEmail().split("@");
                    String maskedEmail = emailParts[0].substring(0, 2) + "***@" + emailParts[1];
                    json.addProperty("email", maskedEmail);
                } else {
                    json.addProperty("email", "");
                }
                json.addProperty("emergencyContact", student.getEmergencyContact() != null ? student.getEmergencyContact() : "");

                json.addProperty("username", student.getUsername());
                json.addProperty("password", "********");
                out.print(json.toString());
            } else {
                resp.sendError(HttpServletResponse.SC_NOT_FOUND, "用户不存在");
            }
        } catch (Exception e) {
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "服务器错误");
            e.printStackTrace();
        }



    }
}



